const tools = {
    getToken() {
        const $token = document.getElementsByName('_tb_token_');
        return $token.length ? $token[0].value : '';
    },
    /**
     * @return {string}
     */
    HTMLDecode(text) {
        let temp = document.createElement("div");
        temp.innerHTML = text;
        let output = temp.innerText || temp.textContent;
        temp = null;
        return output;
    },
    formatUnixtimestamp(unixtimestamp) {
        if (unixtimestamp === null || unixtimestamp === undefined) {
            return "";
        }
        unixtimestamp = new Date(unixtimestamp * 1000);
        const year = 1900 + unixtimestamp.getYear();
        const month = "0" + (unixtimestamp.getMonth() + 1);
        const date = "0" + unixtimestamp.getDate();
        const hour = "0" + unixtimestamp.getHours();
        const minute = "0" + unixtimestamp.getMinutes();
        const second = "0" + unixtimestamp.getSeconds();
        return year + "-" + month.substring(month.length - 2, month.length) + "-" + date.substring(date.length - 2, date.length)
            + " " + hour.substring(hour.length - 2, hour.length) + ":"
            + minute.substring(minute.length - 2, minute.length) + ":"
            + second.substring(second.length - 2, second.length);
    },
    getUrlParam(name) {
        const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`);
        const r = decodeURIComponent(window.location.search.substr(1)).match(reg);
        if (r !== null) return unescape(r[2]);
        return null;
    },
    openUrl(url) {
        let a = document.createElement('a');
        a.setAttribute('href', url);
        a.setAttribute('target', '_blank');
        a.setAttribute('id', "realurl");
        // 防止反复添加
        if (!document.getElementById("realurl")) {
            document.body.appendChild(a);
        }
        a.click();
    },
    isString(str) {
        return typeof str === 'string';
    },
    isArray(object) {
        return object instanceof Array;
    },
    isObject(obj) {
        return this._type(obj) === 'object';
    },
    isFunction(fn) {
        return this._type(fn) === 'function';
    },
    isWindow(obj) {
        return obj !== null && obj === obj.window;
    },
    isDocument(obj) {
        return obj !== null && obj.nodeType === obj.DOCUMENT_NODE;
    },
    isPlainObject(obj) {
        return this.isObject(obj) && !this.isWindow(obj) && Object.getPrototypeOf(obj) === Object.prototype;
    },
    isJsonString(str) {
        try {
            if (typeof JSON.parse(str) === 'object') {
                return true;
            }
        } catch (e) {
        }
        return false;
    },
    getCookie(name) {
        const value = '; ' + document.cookie;
        const parts = value.split('; ' + name + '=');
        if (parts.length === 2) return parts.pop().split(';').shift();
    },
    setCookie(name, value, days) {
        let expires = '';
        if (days) {
            const date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = '; expires=' + date.toUTCString();
        }
        document.cookie = name + '=' + value + expires + '; path=/';
    },
    _type(obj) {
        const class2type = {};
        const toString = class2type.toString;
        return obj === null ? String(obj) :
            class2type[toString.call(obj)] || 'object';
    },
    extend(target, source) {
        target = target || {};
        source = source || {};
        for (const key in source) {
            target.key = source.key;
        }
        return target;
    },
    // 文本过长时省略
    shortStr(Str, num) {
        if (Str && num) {
            if (Str.length > num) {
                return Str.substring(0, num) + '...'
            } else {
                return Str
            }
        } else {
            return ''
        }
    },
    // 中间用逗号隔开的字符串去重
    checkRepeat(str) {
        let res = [], json = {};
        if (str) {
            let arrTemp = str.split(',');
            for (let i = 0; i < arrTemp.length; i++) {
                if (!json[arrTemp[i]]) {
                    res.push(arrTemp[i]);
                    json[arrTemp[i]] = 1;
                }
            }
        }
        return res.join(',')
    },
    namespace(name) {
        return function (v) {
            return `${name}-${v}`;
        };
    },
};

export const NameSpace = tools.namespace.bind(tools);
export default tools;
